package com.atguigu.system.controller;


import com.atguigu.common.result.Result;
import com.atguigu.model.system.SysUser;
import com.atguigu.model.vo.user.*;
import com.atguigu.system.service.SysUserServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author flyduck
 * @since 2023-10-25
 */
@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserServiceImpl sysUserServiceImpl;

    @ApiOperation("获取用户的角色数据")
    @GetMapping("/toAssign/{userId}")
    public Result<SysUserAssignRoleQueryRespVo> toAssign(@PathVariable("userId") Long userId){
        SysUserAssignRoleQueryRespVo respVo = sysUserServiceImpl.getRolesByUserId(userId);
        return Result.ok(respVo);
    }

    @ApiOperation("用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody SysUserAssignRoleReqVo reqVo){
        sysUserServiceImpl.doAssign(reqVo);
        return Result.ok();
    }

    @ApiOperation("更改用户状态")
    @GetMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable("id") Long id,
                               @PathVariable("status") Integer status){
        sysUserServiceImpl.updateStatus(id,status);
        return Result.ok();
    }


    @ApiOperation("条件分页查询")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<SysUserQueryRespVo>> findPageQueryUser(@PathVariable("page") Long page,
                                                               @PathVariable("limit") Long limit,
                                                               SysUserQueryReqVo sysUserQueryReqVo){
        Page<SysUser> pageParam = new Page<>(page, limit);
        IPage<SysUserQueryRespVo> pageModel = sysUserServiceImpl.selectPage(pageParam, sysUserQueryReqVo);
        return Result.ok(pageModel);
    }

    @ApiOperation("添加用户")
    @PostMapping("/save")
    public Result<Void> saveRole(@RequestBody SysUserSaveReqVo sysUserSaveReqVo){
        boolean isSuccess = sysUserServiceImpl.save(sysUserSaveReqVo);
        if (isSuccess) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @ApiOperation("根据id查询")
    @PostMapping("/getUser/{id}")
    public Result<SysUserQueryRespVo> getUser(@PathVariable("id") Long id){
        SysUserQueryRespVo sysUserQueryRespVo = sysUserServiceImpl.findUserById(id);
        return Result.ok(sysUserQueryRespVo);
    }

    @ApiOperation("修改用户")
    @PostMapping("/update")
    public Result<Void> updateUser(@RequestBody SysUserUpdateReqVo sysUserUpdateReqVo){
        boolean isSuccess = sysUserServiceImpl.updateUser(sysUserUpdateReqVo);
        if (isSuccess) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @ApiOperation("根据id逻辑删除用户")
    @DeleteMapping("/remove/{id}")
    public Result<Void> removeUser(@PathVariable("id") Long id){
        boolean isSuccess = sysUserServiceImpl.removeById(id);
        if (isSuccess) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

}

